﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace BS_Gimnasio.Seguridad
{
    public abstract class RemoteUpload
    {
        public string FileName
        {
            get { return FileName; }
            set { FileName = value; }
        }

        public string UrlString
        {
            get { return UrlString; }
            set { UrlString = value; }
        }

        public string NewFileName
        {
            get { return NewFileName; }
            set { NewFileName = value; }
        }

        public byte[] FileData
        {
            get { return FileData; }
            set { FileData = value; }
        }

        public string Usuario
        {
            get { return Usuario; }
            set { Usuario = value; }
        }

        public string Password
        {
            get { return Password; }
            set { Password = value; }
        }

        public RemoteUpload(byte[] fileData, string fileName, string urlString, string usuario, string password)
        {
            this.FileData = fileData;
            this.FileName = fileName;
            this.Usuario = usuario;
            this.Password = password;
            this.UrlString = urlString;
        }

        public virtual bool UploadFile()
        {
            return true;
        }
    }

    public class HttpRemoteUpload : RemoteUpload
    {
        public HttpRemoteUpload(byte[] fileData, string fileNamePath, string urlString, string usuario, string password)
            : base(fileData, fileNamePath, urlString, usuario, password)
        {

        }

        public override bool UploadFile()
        {
            byte[] postData;
            try
            {
                postData = this.FileData;
                using (WebClient client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultCredentials;
                    client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                    client.UploadData(this.UrlString, "PUT", postData);
                }

                return true;
            }

            catch (Exception ex)
            {
                throw new Exception("Failed to upload", ex.InnerException);
            }

        }
    }

    public class FtpRemoteUpload : RemoteUpload
    {
        public FtpRemoteUpload(byte[] fileData, string fileNamePath, string urlString, string usuario, string password)
            : base(fileData, fileNamePath, urlString, usuario, password)
        {

        }

        public override bool UploadFile()
        {
            try
            {
                FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(this.UrlString);
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                reqFTP.Credentials = new NetworkCredential(Usuario, Password);
                reqFTP.UsePassive = true;
                reqFTP.UseBinary = true;
                reqFTP.KeepAlive = true;

                byte[] buff = new byte[0];
                MemoryStream streamNuevo = new MemoryStream(this.FileData);
                Stream reqStream = reqFTP.GetRequestStream();
                reqStream.Write(FileData, 0, FileData.Length);
                reqStream.Flush();
                reqStream.Close();
                return true;
            }

            catch (Exception ex)
            {
                Console.Write("Failed to upload", ex.InnerException);
                return false;
            }
        }
    }

    public class Document
    {
        public int DocId
        {
            get { return DocId; }
            set { DocId = value; }
        }

        public string DocName
        {
            get { return DocName; }
            set { DocName = value; }
        }

        public byte[] DocContent
        {
            get { return DocContent; }
            set { DocContent = value; }
        }
    }
}
